//
//  SelectorGroup.m
//  etionUI
//
//  Created by wangjian on 9/26/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "SelectorGroup.h"

#define SELECTOR_ITEM_H 28
#define SELECTOR_ITEM_FONT_H 14
#define SELECTOR_MARGIN 20

@implementation CSelectorItem
{
    UIImage *_unselectImage;
    UIImage *_selectImage;
    UIImageView *_imageView;
}

- (id)initWithFrame:(CGRect)frame selectImage:(UIImage *)selectImage unselectImage:(UIImage *)unselectImage imageStartX:(float)startX
{
    if (self = [super initWithFrame:frame])
    {
        _selectImage = [selectImage retain];
        _unselectImage = [unselectImage retain];
        
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(startX, (self.height-selectImage.size.height)/2, selectImage.size.width, selectImage.size.height)];
        _imageView.image = unselectImage;
        [self addSubview:_imageView];
    }
    return self;
}

- (void)dealloc
{
    [_selectImage release];
    [_unselectImage release];
    [_titleLable release];
    [_imageView release];
    [super dealloc];
}

- (UILabel *)titleLable
{
    if (_titleLable == nil)
    {
        _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _imageView.left, self.height)];
        _titleLable.backgroundColor = [UIColor clearColor];
        _titleLable.font = [UIFont systemFontOfSize:SELECTOR_ITEM_FONT_H];
        _titleLable.textColor = [UIColor colorWithHex:0x494e56];
        [self addSubview:_titleLable];
    }
    return _titleLable;
}

- (void)setSelected:(BOOL)selected
{
    if (selected == _selected)
    {
        return;
    }
    
    _selected = selected;
    _imageView.image = _selected ? _selectImage : _unselectImage;
}

@end

@implementation CSelectorGroup
{
    NSUInteger itemNumPerLine;
    float itemWidth;
    float imageStartX;
}

@synthesize delegate = _delegate;

- (CGRect)frameWithTitles:(NSArray *)titles width:(float)width selectImage:(UIImage *)selectImage
{
    __block NSString *maxLenTitle = nil;
    for (NSString *title in titles)
    {
        if (title.length > maxLenTitle.length)
        {
            maxLenTitle = title;
        }
    }
    
    float maxTitleWidth = [maxLenTitle stringSizeWithFont:[UIFont systemFontOfSize:14]].width;
    float maxWidth = maxTitleWidth + selectImage.size.width + SELECTOR_MARGIN;
    itemNumPerLine = width/maxWidth;
    if (itemNumPerLine > 3)
    {
        itemNumPerLine = 3;
    }
    if (itemNumPerLine == 0)
    {
        itemNumPerLine = 1;
    }
    if (itemNumPerLine > titles.count)
    {
        itemNumPerLine = titles.count;
    }
    itemWidth = (width-(itemNumPerLine-1)*SELECTOR_MARGIN)/itemNumPerLine;
    imageStartX = itemWidth-selectImage.size.width;
    if (itemWidth-selectImage.size.width - maxTitleWidth > SELECTOR_MARGIN)
    {
        imageStartX = maxTitleWidth + SELECTOR_MARGIN;
    }
    
    if (itemNumPerLine == 0)
    {
        return CGRectMake(0, 0, width, SELECTOR_ITEM_H);
    }
    return CGRectMake(0, 0, width, (titles.count+(itemNumPerLine-1))/itemNumPerLine * SELECTOR_ITEM_H);
}

- (id)initWithTitles:(NSArray *)titles width:(float)width selectImage:(UIImage *)selectImage unselectImage:(UIImage *)unselectImage
{
    if (self = [super initWithFrame:[self frameWithTitles:titles width:width selectImage:selectImage]])
    {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureReceive:)];
        [self addGestureRecognizer:tapGesture];
        [tapGesture release];
        
        _arItems = [[NSMutableArray alloc] init];
        for (int i=0; i<titles.count; i++)
        {
            CSelectorItem *item = [[CSelectorItem alloc] initWithFrame:CGRectMake(i%itemNumPerLine*(itemWidth+SELECTOR_MARGIN), i/itemNumPerLine*SELECTOR_ITEM_H, itemWidth, SELECTOR_ITEM_H) selectImage:selectImage unselectImage:unselectImage imageStartX:imageStartX];
            item.titleLable.text =[titles objectAtIndex:i];
            [_arItems addObject:item];
            [self addSubview:item];
            [item release];
        }
    }
    return self;
}

- (void)dealloc
{
    [_arItems release];
    [super dealloc];
}

- (void)changeSelectStateAtIndex:(NSUInteger)index toState:(BOOL)isSelected
{
    CSelectorItem *item = [_arItems objectAtIndex:index];
    item.selected = isSelected;
}

- (void)tapGestureReceive:(UITapGestureRecognizer *)tapGesture
{
    CGPoint tapPoint = [tapGesture locationInView:self];
    NSUInteger tapIndex = (NSUInteger)(tapPoint.y/SELECTOR_ITEM_H)*itemNumPerLine + (NSUInteger)(tapPoint.x/(self.width/itemNumPerLine));
    
    [self tapItemAtIndex:tapIndex];
}

- (void)tapItemAtIndex:(NSUInteger)index;
{
    CSelectorItem *item = [_arItems objectAtIndex:index];
    item.selected = !item.selected;
    if ([self.delegate respondsToSelector:@selector(selectorGroup:itemAtIndex:selectStateChanged:)])
    {
        [self.delegate selectorGroup:self itemAtIndex:index selectStateChanged:item.selected];
    }
}

- (NSIndexSet *)selectedIndexSet
{
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    [_arItems enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        CSelectorItem *item = obj;
        if (item.selected == YES)
        {
            [indexSet addIndex:idx];
        }
    }];
    return indexSet;
}

@end

@implementation CSelectorGroupUnique

- (NSUInteger)currentSelectedIndex
{
    NSIndexSet *indexSet = [self selectedIndexSet];
    __block NSUInteger currentIndex = NSNotFound;
    [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        currentIndex = idx;
        *stop = YES;
    }];
    return currentIndex;
}

- (void)tapItemAtIndex:(NSUInteger)index;
{
    CSelectorItem *item = [_arItems objectAtIndex:index];
    if (item.selected == YES)
    {
        return;
    }
    
    NSUInteger preSelectedIndex = [self currentSelectedIndex];
    if (preSelectedIndex != NSNotFound)
    {
        CSelectorItem *preItem = [_arItems objectAtIndex:preSelectedIndex];
        preItem.selected = NO;
        if ([self.delegate respondsToSelector:@selector(selectorGroup:itemAtIndex:selectStateChanged:)])
        {
            [self.delegate selectorGroup:self itemAtIndex:preSelectedIndex selectStateChanged:preItem.selected];
        }
    }
    
    item.selected = YES;
    if ([self.delegate respondsToSelector:@selector(selectorGroup:itemAtIndex:selectStateChanged:)])
    {
        [self.delegate selectorGroup:self itemAtIndex:index selectStateChanged:item.selected];
    }
}

@end

@implementation CSelectorGroupMultiple



@end
